package controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

import weka.core.converters.CSVSaver;
import model.Call;
import model.Contact;
import model.Participant;
import model.Sms;
import model.Statistics;
import model.User;

public class DataProcessing {
	int dem = 0;
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSetContactList = null, resultSetCallLogs = null,
			resultSetSmsLogs = null, resultSetUserData = null,
			resutlSetParticipant = null;
	private ArrayList<Contact> arrContact;
	private ArrayList<Call> arrCall;
	private ArrayList<Sms> arrSms;
	// private HashMap<String, User> hashMapUser;
	private ArrayList<User> arrUser, arrUserStats;
	private ArrayList<Statistics> arrStatistic;
	private ArrayList<Participant> arrParticipant;

	private void getParticipantResultSet(ResultSet resultSet)
			throws SQLException {
		int count = 0;
		while (resultSet.next()) {
			String imei = resultSet.getString("imei");
			String name = resultSet.getString("name");
			String age = resultSet.getString("age");
			String job = resultSet.getString("job");
			String gender = resultSet.getString("gender");

			Participant part = new Participant(imei, name, age, job, gender);
			arrParticipant.add(part);
		}
		resultSet.close();

	}

	private void getContactListResultSet(ResultSet resultSet)
			throws SQLException {
		int count = 0;
		while (resultSet.next()) {
			String imei = resultSet.getString("imei");
			String contactid = resultSet.getString("contactid");
			String phonetype = resultSet.getString("phonetype");
			String email = resultSet.getString("email");
			String emailtype = resultSet.getString("emailtype");
			String address = resultSet.getString("address");
			String addresstype = resultSet.getString("addresstype");
			String note = resultSet.getString("note");
			String title = resultSet.getString("note");
			String company = resultSet.getString("company");
			String im = resultSet.getString("im");
			String imtype = resultSet.getString("imtype");
			String isstared = resultSet.getString("isstared");
			String hasphoto = resultSet.getString("hasphoto");
			String hasanniversary = resultSet.getString("hasanniversary");
			String agecompare = resultSet.getString("agecompare");
			String facebookfriend = resultSet.getString("facebookfriend");
			String gender = resultSet.getString("gender");
			String frequentseen = resultSet.getString("seen");
			String family = resultSet.getString("family");
			String work = resultSet.getString("work");
			String social = resultSet.getString("social");
			String friend = resultSet.getString("friend");

			Contact contact = new Contact(imei, contactid, phonetype, email,
					emailtype, address, addresstype, note, title, company, im,
					imtype, isstared, hasphoto, hasanniversary, agecompare,
					facebookfriend, gender, frequentseen, family, work, social,
					friend);
			if (!(family + work + social + friend).equals("0000")) {
				arrContact.add(contact);
			}
		}
		resultSet.close();

	}

	private void getCallResultSet(ResultSet resultSet1) throws SQLException {
		while (resultSet1.next()) {
			String imei = resultSet1.getString("imei");
			String contactid = resultSet1.getString("contactid");
			String calltype = resultSet1.getString("calltype");
			int duration = resultSet1.getInt("duration");
			java.sql.Date date = resultSet1.getDate("datetime");
			java.sql.Time time = resultSet1.getTime("datetime");

			if (calltype.equals(Call.INCOMING) && duration == 0) {
				calltype = Call.MISSED;
			}
			Call call = new Call(imei, contactid, date, time, duration,
					calltype);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			int week = cal.get(Calendar.WEEK_OF_YEAR);
			call.setWeek(week);

			if (duration < 3) {
				call.setCalltype(Call.FAILED);
			}
			arrCall.add(call);
		}
		resultSet1.close();
	}

	private void getSmsResultSet(ResultSet resultSet2) throws SQLException {
		while (resultSet2.next()) {
			String imei = resultSet2.getString("imei");
			String contactid = resultSet2.getString("contactid");
			String threadid = resultSet2.getString("threadid");
			String smstype = resultSet2.getString("smstype");
			java.sql.Date date = resultSet2.getDate("datetime");
			java.sql.Time time = resultSet2.getTime("datetime");

			int length = resultSet2.getInt("length");
			Sms sms = new Sms(imei, contactid, date, time, threadid, length,
					smstype);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			int week = cal.get(Calendar.WEEK_OF_YEAR);
			sms.setWeek(week);
			arrSms.add(sms);
		}
		resultSet2.close();
	}

	// You need to close the resultSet
	private void close(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}

			if (statement != null) {
				statement.close();
			}

			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	}

	public DataProcessing() {
		super();
		arrContact = new ArrayList<>();
		arrCall = new ArrayList<>();
		arrSms = new ArrayList<>();
		arrUser = new ArrayList<>();
		arrUserStats = new ArrayList<>();
		arrStatistic = new ArrayList<>();
		arrParticipant = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			Statistics stats = new Statistics();
			arrStatistic.add(stats);
		}

	}

	public void readDataBase() throws Exception {
		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			// Setup the connection with the DB
			connect = DriverManager
					.getConnection("jdbc:mysql://localhost/relationshipmining2?"
							+ "user=root&password=");

			// Statements allow to issue SQL queries to the database
			statement = connect.createStatement();
			// Result set get the result of the SQL query
			resultSetContactList = statement
					.executeQuery("select * from contactdata");
			statement = connect.createStatement();
			resultSetCallLogs = statement
					.executeQuery("select * from phonelogs");
			statement = connect.createStatement();
			resultSetSmsLogs = statement.executeQuery("select * from smslogs");

			statement = connect.createStatement();
			resutlSetParticipant = statement
					.executeQuery("select * from userdata");

			getContactListResultSet(resultSetContactList);
			getCallResultSet(resultSetCallLogs);
			getSmsResultSet(resultSetSmsLogs);
			getParticipantResultSet(resutlSetParticipant);
		} catch (Exception e) {
			throw e;

		} finally {
			close(resultSetContactList);
			close(resultSetCallLogs);
			close(resultSetSmsLogs);
			close(resultSetUserData);
			close(resutlSetParticipant);
		}
	}

	public void process() {
		for (int j = 0; j < arrContact.size(); j++) {
			User user = new User(arrContact.get(j));
			String label = arrContact.get(j).getFamily()
					+ arrContact.get(j).getWork()
					+ arrContact.get(j).getSocial()
					+ arrContact.get(j).getFriend();
			user.setRelationshipLabel(label);

			// ~Call Logs---------------------------------------------------
			int hour[] = new int[24];
			for (int i = 0; i < arrCall.size(); i++) {
				if (arrCall.get(i).getImei()
						.equals(arrContact.get(j).getImei())
						&& arrCall.get(i).getContactid()
								.equals(arrContact.get(j).getContactid())
						&& !checkTime(arrCall.get(i).getDate())) {
					// ----------------STATISTIC RELATION ----------------------
					if (user.relationshipLabelProcessing().equals("Gia_dinh")) {
						arrStatistic.get(0).increaseCallHour(
								arrCall.get(i).getTime().getHours());
						arrStatistic.get(0).increaseCallnum();
					} else if (user.relationshipLabelProcessing().equals(
							"Cong_viec")) {
						arrStatistic.get(1).increaseCallnum();
						arrStatistic.get(1).increaseCallHour(
								arrCall.get(i).getTime().getHours());
					} else if (user.relationshipLabelProcessing().equals(
							"Xa_hoi")) {
						arrStatistic.get(2).increaseCallnum();
						arrStatistic.get(2).increaseCallHour(
								arrCall.get(i).getTime().getHours());
					} else {
						arrStatistic.get(3).increaseCallnum();
						arrStatistic.get(3).increaseCallHour(
								arrCall.get(i).getTime().getHours());
					}
					user.increaseCallCount();
					user.addCallLogs(arrCall.get(i));
				}
			}

			// ~End of Call Logs---------------------Start of Sms Logs
			for (int i = 0; i < arrSms.size(); i++) {

				if (arrSms.get(i).getImei().equals(arrContact.get(j).getImei())
						&& arrSms.get(i).getContactid()
								.equals(arrContact.get(j).getContactid())
						&& !checkTime(arrSms.get(i).getDate())) {
					if (user.relationshipLabelProcessing().equals("Gia_dinh")) {
						arrStatistic.get(0).increaseSmsnum();
						arrStatistic.get(0).increaseSmsHour(
								arrSms.get(i).getTime().getHours());

					} else if (user.relationshipLabelProcessing().equals(
							"Cong_viec")) {
						arrStatistic.get(1).increaseSmsnum();

						arrStatistic.get(1).increaseSmsHour(
								arrSms.get(i).getTime().getHours());
					} else if (user.relationshipLabelProcessing().equals(
							"Xa_hoi")) {
						arrStatistic.get(2).increaseSmsnum();

						arrStatistic.get(2).increaseSmsHour(
								arrSms.get(i).getTime().getHours());
					} else {
						arrStatistic.get(3).increaseSmsnum();

						arrStatistic.get(3).increaseSmsHour(
								arrSms.get(i).getTime().getHours());

					}
					user.increaseSmsCount();
					user.addSmsLogs(arrSms.get(i));
				}

			}
			user.setImei(arrContact.get(j).getImei());
			user.setId(arrContact.get(j).getContactid());

			// ~Gán đồng giới
			for (int k = 0; k < arrParticipant.size(); k++) {

				if (arrParticipant.get(k).getImei().equals(user.getImei())) {

					if (arrParticipant.get(k).getGender()
							.equals(arrContact.get(j).getGender())) {
						user.setSamegender(1);
					}
				}
			}

			if (user != null
					&& (user.getArrCallLogs().size() > 0 || user
							.getArrSmsLogs().size() > 0)) {
				arrUserStats.add(user);
				if ((user.getRelationshipLabel().equals("1000")
						|| user.getRelationshipLabel().equals("0100")
						|| user.getRelationshipLabel().equals("0010") || user
						.getRelationshipLabel().equals("0001"))) {
					arrUser.add(user);
				}
			}
		}

		System.out.println(arrUserStats.size());
		// ~ Load user vào từng participant
		for (int i = 0; i < arrUser.size(); i++) {
			for (int j = 0; j < arrParticipant.size(); j++) {
				if (arrParticipant.get(j).getImei()
						.equals(arrUser.get(i).getImei())) {
					arrParticipant.get(j).addUser(arrUser.get(i));
				}
			}
		}

		// Tính giá trị tuyệt đối giữa SMS of a contact and average number of
		// SMS of a participant
		for (int r = 0; r < arrParticipant.size(); r++) {
			double avg_sms = 0, avg_call = 0;
			// Tính average của 1 participant
			int total_call = 0, total_sms = 0;
			for (int t = 0; t < arrParticipant.get(r).getArraypUser().size(); t++) {
				for (int n = 0; n < arrParticipant.get(r).getArraypUser()
						.get(t).getArrCallLogs().size(); n++) {
					total_call++;
				}

				for (int m = 0; m < arrParticipant.get(r).getArraypUser()
						.get(t).getArrSmsLogs().size(); m++) {
					total_sms++;
				}
			}

			avg_call = (double) total_call
					/ (double) arrParticipant.get(r).getArraypUser().size();
			avg_sms = (double) total_sms
					/ (double) arrParticipant.get(r).getArraypUser().size();

			for (int j = 0; j < arrParticipant.get(r).getArraypUser().size(); j++) {
				int num_call = arrParticipant.get(r).getArraypUser().get(j)
						.getArrCallLogs().size();
				int num_sms = arrParticipant.get(r).getArraypUser().get(j)
						.getArrSmsLogs().size();

				arrParticipant
						.get(r)
						.getArraypUser()
						.get(j)
						.setDifferentBettweenCall(Math.abs(num_call - avg_call));
				arrParticipant.get(r).getArraypUser().get(j)
						.setDifferentBetweenSms(Math.abs(num_sms - avg_sms));
			}

		}

		for (int r = 0; r < arrParticipant.size(); r++) {
			arrParticipant.get(r).calcBeginAndEndDateCommunicationLog();
		}
		// Sắp xếp từng participant
		final int t = 10;
		for (int i = 0; i < arrParticipant.size(); i++) {
			arrParticipant.get(i).sortDescendingUserByCall();
			int g = 1;
			for (int j = 0; j < arrParticipant.get(i).getArraypUser().size(); j = j
					+ t) {
				int k = j + t;
				if (k > arrParticipant.get(i).getArraypUser().size()) {
					k = arrParticipant.get(i).getArraypUser().size();
				}
				for (int n = j; n < k; n++) {
					arrParticipant.get(i).getArraypUser().get(n).setCallrank(g);
				}
				g++;
			}

			arrParticipant.get(i).sortDescendingUserBySms();

			g = 1;
			for (int j = 0; j < arrParticipant.get(i).getArraypUser().size(); j = j
					+ t) {
				int k = j + t;
				if (k > arrParticipant.get(i).getArraypUser().size()) {
					k = arrParticipant.get(i).getArraypUser().size();
				}
				for (int n = j; n < k; n++) {
					arrParticipant.get(i).getArraypUser().get(n).setSmsrank(g);
				}
				g++;
			}
		}

		for (int i = 0; i < arrUser.size(); i++) {
			for (int j = 0; j < arrParticipant.size(); j++) {
				if (arrUser.get(i).getImei()
						.equals(arrParticipant.get(j).getImei())) {
					for (int k = 0; k < arrParticipant.get(j).getArraypUser()
							.size(); k++) {
						if (arrUser
								.get(i)
								.getId()
								.equals(arrParticipant.get(j).getArraypUser()
										.get(k).getId())) {
							arrUser.get(i).setCallrank(
									arrParticipant.get(j).getArraypUser()
											.get(k).getCallrank());
							arrUser.get(i).setSmsrank(
									arrParticipant.get(j).getArraypUser()
											.get(k).getSmsrank());
							arrUser.get(i).setDifferentBettweenCall(
									arrParticipant.get(j).getArraypUser()
											.get(k).getDifferentBettweenCall());
							arrUser.get(i).setDifferentBetweenSms(
									arrParticipant.get(j).getArraypUser()
											.get(k).getDifferentBetweenSms());
							arrUser.get(i).setNumDayLogs(
									arrParticipant.get(j).getNumDayLogs());
						}
					}
				}

			}
		}

	}

	/**
	 * Export Statistics
	 * 
	 * @throws IOException
	 */
	public void exportStatistic() throws IOException {
		int call_activity = 0, sms_activity = 0;
		for (int t = 0; t < arrUser.size(); t++) {
			call_activity += arrUser.get(t).getArrCallLogs().size();
			sms_activity += arrUser.get(t).getArrSmsLogs().size();
			arrUser.get(t).calcOutCOMMs_COMMsANDcalls_COMMs();
			arrUser.get(t).calc_avg_max_dur_out_in_call();
			arrUser.get(t).calc_AVG_STD_MIN_MAX_MED_SMS_CALL_per_Week();
			arrUser.get(t).calcMaintaincost();
			arrUser.get(t).calcSms_duration_total();
			arrUser.get(t).calcNumCallSMSCOMMSAtTimeOfDay();
			arrUser.get(t).calcNumReplySmsVTotalProb();
			arrUser.get(t).calcNumDayCall_SMS();
			arrUser.get(t).calcAverageDurationCallAtDayOfWeek();
		}
		FileWriter fw = new FileWriter(new File("output.csv"));
		int sms_am = 0, call_am = 0, numCallLengthly = 0, samegender = 0, callRank, smsRank;
		double outcomms_comms, calls_comms, diff_bw_sms, diff_bw_call, weighted_sms, weighted_call, all_sms_average = 0, all_call_average = 0, durAver, lenAver, smsCallProb, out_total_call_prob, dur_outcall_total_prob = 0, outsmstotal = 0, outsmslentotal = 0;
		String label, mostDayInWk, callmostDayInWk, smsMostDayInWk, commMostTimeInDay, diffage, facebookfriend;
		String isstared, hasphoto, hasanniversary, frequentseen;
		all_sms_average = (double) sms_activity / arrUser.size();
		all_call_average = (double) call_activity / arrUser.size();
		int workphone = 0, homephone = 0, email = 0, note = 0, im = 0;
		double avg_out_call_per_week, std_out_call_per_week, min_out_call_per_week, max_out_call_per_week, med_out_call_per_week;
		double avg_in_call_per_week, std_in_call_per_week, min_in_call_per_week, max_in_call_per_week, med_in_call_per_week;
		double avg_out_sms_per_week, std_out_sms_per_week, min_out_sms_per_week, max_out_sms_per_week, med_out_sms_per_week;
		double avg_in_sms_per_week, std_in_sms_per_week, min_in_sms_per_week, max_in_sms_per_week, med_in_sms_per_week;
		double avg_outgoing_call, avg_dur_in_call;
		double max_dur_out_call, max_dur_in_call;
		double failedcalloutcall, missedcalloutcall;
		double totalCallV2WeekTotal, totalCallV2MonthTotal, totalSMSV2WeekTotal, totalSMSV2MonthTotal, durCallV2MonthTotal, durCallV2WeekTotal, lenSMSV2MonthTotal, lenSMSV2WeekTotal, COMMS2WEEKTOTAL, COMMS2MONTHTOTAL;
		double dayCallVdayLogged, daySmsVdayLogged, dayCOMMsVdayLogged;
		fw.write("\"avg-out-call/week\",\"avg-in-call/week\",\"avg-out-sms/week\",\"avg-in-sms/week\","
				+ "\"std-out-call/week\",\"std-in-call/week\",\"std-out-sms/week\",\"std-in-sms/week\","
				// +
				// "\"min-out-call/week\",\"min-in-call/week\",\"min-out-sms/week\",\"min-in-sms/week\","
				+ "\"max-out-call/week\",\"max-in-call/week\",\"max-out-sms/week\",\"max-in-sms/week\","
				+ "\"med-out-call/week\",\"med-in-call/week\",\"med-out-sms/week\",\"med-in-sms/week\","
				+ "\"avg-dur-in-call/week\",\"avg-dur-out-call/week\",\"max-dur-in-call/week\",\"max-dur-out-call/week\","
				+ "\"#2-week-call/#calls\",\"#2-month-call/#calls\",\"#2-week-sms/#calls\","
				+ "\"#2-month-sms/#smss\",\"dur-2-month-call/dur-calls\",\"dur-2-week-call/dur-#calls\","
				+ "\"len-2-month-sms/len-smss\",\"len-2-week-sms/lensmss\",\"#2-week-COMMs/COMMs\","
				+ "\"#2-month-COMMs/total\","
				+ "\"#failed-call/#out-call\","
				+ "\"#missed-call/#in-call\","
				+ "\"#replied-sms/#smss\","
				+ "\"#daycall/#daylogged\","
				+ "\"#daysms/#daylogged\","
				+ "\"#dayCOMMs/#daylogged\","
				+ "\"#out-COMMs/#COMMs\",\"#calls/#COMMs\",\"#calls\",\"#smss\",\"dur-calls\","
				+ "\"len-smss\",\"#out-call/#calls\",\"dur-out-call/dur-calls\",\"#out-sms/#smss\",\"len-out-sms/len-smss\","
				+ "\"|#call-(avg-calls)|\",\"|#sms-(avg-smss)|\","
				+ "\"#smss+#calls\",\"avg-len-sms\",\"avg-dur-call\",\"#lenghly-dur-call\","
//				+ "\"COMMs-day-in-week\",\"call-day-in-week\","
				+"\"call-rank\",\"sms-rank\","
//				+"\"sms-day-in-week\",\"time-in-day\","

				+ "\"#call-at-morning/#calls\",\"#call-at-afternoon/#calls\",\"#call-at-evening/#calls\",\"#call-at-night/#calls\","

				+ "\"#sms-at-morning/#smss\",\"#sms-at-afternoon/#smss\",\"#sms-at-evening/#smss\",\"#sms-at-night/#smss\","
				+ "\"#comm-at-morning/#comms\",\"#comm-at-afternoon/#comms\",\"#comm-at-evening/#comms\",\"#comm-at-night/#comms\","
				+ "\"dur-call-at-morning/dur-calls\",\"dur-call-at-afternoon/dur-calls\",\"dur-call-at-evening/dur-calls\",\"dur-call-at-night/dur-calls\","
				+ "\"len-sms-at-morning/len-smss\",\"len-sms-at-afternoon/len-smss\",\"len-sms-at-evening/len-smss\",\"len-sms-at-night/len-smss\","
				+ "\"dur-call-at-sunday/dur-calls\",\"dur-call-at-saturday/dur-calls\",\"dur-call-at-friday/dur-calls\",\"dur-call-at-workday/dur-calls\","
				+ "\"#call-at-sunday/#callss\",\"#call-at-saturday/#callss\",\"#call-at-friday/#callss\",\"#call-at-workday/#callss\","
				+ "\"len-sms-at-sunday/len-smss\",\"len-sms-at-saturday/len-smss\",\"len-sms-at-friday/len-smss\",\"len-sms-at-workday/len-smss\","

				+ "\"#sms-at-sunday/#smsss\",\"#sms-at-saturday/#smsss\",\"#sms-at-friday/#smsss\",\"#sms-at-workday/#smsss\","
				+ "\"age-difference-with-user\",\"is-facebook-friend\",\"has-aniversary\","

				+ "\"is-workphone\",\"is-homephone\",\"has-email\",\"has-note\",\"has-im\",\"same-gender-with-user\",\"frequent-seen\",\"class\"\n");
		for (int t = 0; t < arrUser.size(); t++) {

			avg_out_call_per_week = arrUser.get(t).getAvg_out_call_per_week();
			std_out_call_per_week = arrUser.get(t).getStd_out_call_per_week();
			// min_out_call_per_week =
			// arrUser.get(t).getMin_out_call_per_week();
			max_out_call_per_week = arrUser.get(t).getMax_out_call_per_week();
			med_out_call_per_week = arrUser.get(t).getMed_out_call_per_week();
			avg_in_call_per_week = arrUser.get(t).getAvg_in_call_per_week();
			std_in_call_per_week = arrUser.get(t).getStd_in_call_per_week();
			// min_in_call_per_week = arrUser.get(t).getMin_in_call_per_week();
			max_in_call_per_week = arrUser.get(t).getMax_in_call_per_week();
			med_in_call_per_week = arrUser.get(t).getMed_in_call_per_week();
			avg_out_sms_per_week = arrUser.get(t).getAvg_out_sms_per_week();
			std_out_sms_per_week = arrUser.get(t).getStd_out_sms_per_week();
			// min_out_sms_per_week = arrUser.get(t).getMin_out_sms_per_week();
			max_out_sms_per_week = arrUser.get(t).getMax_out_sms_per_week();
			med_out_sms_per_week = arrUser.get(t).getMed_out_sms_per_week();
			avg_in_sms_per_week = arrUser.get(t).getAvg_in_sms_per_week();
			std_in_sms_per_week = arrUser.get(t).getStd_in_sms_per_week();
			// min_in_sms_per_week = arrUser.get(t).getMin_in_sms_per_week();
			max_in_sms_per_week = arrUser.get(t).getMax_in_sms_per_week();
			med_in_sms_per_week = arrUser.get(t).getMed_in_sms_per_week();

			avg_outgoing_call = arrUser.get(t).getAvg_dur_outgoing_call();
			avg_dur_in_call = arrUser.get(t).getAvg_dur_incoming_call();
			max_dur_out_call = arrUser.get(t).getMax_dur_outgoing_call();
			max_dur_in_call = arrUser.get(t).getMax_dur_incoming_call();

			totalCallV2WeekTotal = arrUser.get(t).getTotalCallV2WeekTotal();
			totalCallV2MonthTotal = arrUser.get(t).getTotalCallV2MonthTotal();

			totalSMSV2WeekTotal = arrUser.get(t).getTotalSMSV2WeekTotal();
			totalSMSV2MonthTotal = arrUser.get(t).getTotalSMSV2MonthTotal();
			durCallV2MonthTotal = arrUser.get(t).getDurCallV2MonthTotal();
			durCallV2WeekTotal = arrUser.get(t).getDurCallV2WeekTotal();
			lenSMSV2MonthTotal = arrUser.get(t).getLenSMSV2MonthTotal();
			lenSMSV2WeekTotal = arrUser.get(t).getLenSMSV2WeekTotal();
			COMMS2WEEKTOTAL = arrUser.get(t).getCOMMS2WEEKTOTAL();
			COMMS2MONTHTOTAL = arrUser.get(t).getCOMMS2MONTHTOTAL();
			outcomms_comms = arrUser.get(t).getOutCOMMs_COMMs();
			calls_comms = arrUser.get(t).getCalls_COMMs();
			arrUser.get(t).calcDurOutgoingCallTotalProb();
			out_total_call_prob = arrUser.get(t).getOutgoingcall_totalprob();
			dur_outcall_total_prob = arrUser.get(t)
					.getDuroutgoingcall_totalprob();
			outsmstotal = arrUser.get(t).getOutSmsTotal();
			outsmslentotal = arrUser.get(t).getOutSmsLenTotal();
			failedcalloutcall = arrUser.get(t).getFailcall_outcall();
			missedcalloutcall = arrUser.get(t).getMisscall_incall();
			dayCallVdayLogged = arrUser.get(t).getDayCallVDayLogged();
			daySmsVdayLogged = arrUser.get(t).getDaySmsVdayLogged();
			dayCOMMsVdayLogged = arrUser.get(t).getDayCommsVdayLogged();
			sms_am = arrUser.get(t).getSms_amount();
			call_am = arrUser.get(t).getCall_amount();
			diff_bw_call = arrUser.get(t).getDifferentBettweenCall();
			diff_bw_sms = arrUser.get(t).getDifferentBetweenSms();
			// weighted_sms = (double) sms_am / (double) all_sms_average;
			// weighted_call = (double) call_am / (double) all_call_average;
			durAver = arrUser.get(t).calcCallDurationAverage();
			numCallLengthly = arrUser.get(t).calcLengthlyCall();
			lenAver = arrUser.get(t).calcSmsLengthAverage();
			commMostTimeInDay = arrUser.get(t).calcMostHourOfDay();
			mostDayInWk = arrUser.get(t).calcMostDayInWeek();
			callmostDayInWk = arrUser.get(t).calcCallMostDayInWeek();
			callRank = arrUser.get(t).getCallrank();
			smsRank = arrUser.get(t).getSmsrank();
			smsMostDayInWk = arrUser.get(t).calcSmsMostDayInWeek();
			diffage = arrUser.get(t).getContactdetail().getAgecompare();
			facebookfriend = arrUser.get(t).getContactdetail()
					.getFacebookfriend();
			hasanniversary = arrUser.get(t).getContactdetail()
					.getHasanniversary();
			if (arrUser.get(t).getContactdetail().getPhonetype().equals("Home")) {
				homephone = 1;
			} else if (arrUser.get(t).getContactdetail().getPhonetype()
					.equals("Work")) {
				workphone = 1;
			}
			if (arrUser.get(t).getContactdetail().getEmail().length() > 2) {
				email = 1;
			}
			if (arrUser.get(t).getContactdetail().getNote().length() > 2) {
				note = 1;
			}
			if (arrUser.get(t).getContactdetail().getIm().length() > 3) {
				im = 1;
			} else {
				im = 0;
			}
			samegender = arrUser.get(t).getSamegender();
			frequentseen = arrUser.get(t).getContactdetail().getFrequentseen();
			label = arrUser.get(t).relationshipLabelProcessing();

			fw.write(avg_out_call_per_week
					+ ","
					+ avg_in_call_per_week
					+ ","
					+ avg_out_sms_per_week
					+ ","
					+ avg_in_sms_per_week
					+ ","
					+ std_out_call_per_week
					+ ","
					+ std_in_call_per_week
					+ ","
					+ std_out_sms_per_week
					+ ","
					+ std_in_sms_per_week
					+ ","
					// + min_out_call_per_week + "," + min_in_call_per_week +
					// ","
					// + min_out_sms_per_week + "," + min_in_sms_per_week + ","
					+ max_out_call_per_week
					+ ","
					+ max_in_call_per_week
					+ ","
					+ max_out_sms_per_week
					+ ","
					+ max_in_sms_per_week
					+ ","
					+ med_out_call_per_week
					+ ","
					+ med_in_call_per_week
					+ ","
					+ med_out_sms_per_week
					+ ","
					+ med_in_sms_per_week
					+ ","

					+ avg_dur_in_call
					+ ","
					+ avg_outgoing_call
					+ ","
					+ max_dur_in_call
					+ ","
					+ max_dur_out_call
					+ ","
					+ totalCallV2WeekTotal
					+ ","
					+ totalCallV2MonthTotal
					+ ","
					+ totalSMSV2WeekTotal
					+ ","
					+ totalSMSV2MonthTotal
					+ ","
					+ durCallV2MonthTotal
					+ ","
					+ durCallV2WeekTotal
					+ ","
					+ lenSMSV2MonthTotal
					+ ","
					+ lenSMSV2WeekTotal
					+ ","
					+ COMMS2WEEKTOTAL
					+ ","
					+ COMMS2MONTHTOTAL
					+ ","
					+ failedcalloutcall
					+ ","
					+ missedcalloutcall
					+ ","
					+ arrUser.get(t).getNumRepSmsVtotalProb()
					+ ","
					+ dayCallVdayLogged
					+ ","
					+ daySmsVdayLogged
					+ ","
					+ dayCOMMsVdayLogged
					+ ","
					+ outcomms_comms
					+ ","
					+ calls_comms
					+ ","
					+ arrUser.get(t).getCall_amount()
					+ ","
					+ arrUser.get(t).getSms_amount()
					+ ","
					+ arrUser.get(t).calcCall_duration_total()
					+ ","
					+ arrUser.get(t).getTotalSmsLen()
					+ ","
					+ out_total_call_prob
					+ ","
					+ dur_outcall_total_prob
					+ ","
					+ outsmstotal
					+ ","
					+ outsmslentotal
					+ ","
					+ diff_bw_call
					+ ","
					+ diff_bw_sms
					+ ","
					// + weighted_sms + ","
					// + weighted_call + ","
					+ (sms_am + call_am) + "," + lenAver + "," + durAver + ","
					+ numCallLengthly + "," 
//					+ mostDayInWk + ","
//					+ callmostDayInWk + "," 
					+ callRank + "," + smsRank + ","
//					+ smsMostDayInWk + "," 
//					+ commMostTimeInDay + ","
					+ arrUser.get(t).getNumCallAtMorning_Total() + ","
					+ arrUser.get(t).getNumCallAtAfternoon_Total() + ","
					+ arrUser.get(t).getNumCallAtEvening_Total() + ","
					+ arrUser.get(t).getNumCallAtNight_Total() + ","
					+ arrUser.get(t).getNumSmsAtMorning_Total() + ","
					+ arrUser.get(t).getNumSmsAtAfternoon_Total() + ","
					+ arrUser.get(t).getNumSmsAtEvening_Total() + ","
					+ arrUser.get(t).getNumSmsAtNight_Total() + ","
					+ arrUser.get(t).getNumCommAtMorning_Total() + ","
					+ arrUser.get(t).getNumCommAtAfternoon_Total() + ","
					+ arrUser.get(t).getNumCommAtEvening_Total() + ","
					+ arrUser.get(t).getNumCommAtNight_Total() + ","

					+ arrUser.get(t).getDurCallAtMorning_Total() + ","
					+ arrUser.get(t).getDurCallAtAfternoon_Total() + ","
					+ arrUser.get(t).getDurCallAtEvening_Total() + ","
					+ arrUser.get(t).getDurCallAtNight_Total() + ","

					+ arrUser.get(t).getLenSmsAtMorning_Total() + ","
					+ arrUser.get(t).getLenSmsAtAfternoon_Total() + ","
					+ arrUser.get(t).getLenSmsAtEvening_Total() + ","
					+ arrUser.get(t).getLenSmsAtNight_Total() + ","

					+ arrUser.get(t).getAvg_dur_at_sunday() + ","
					+ arrUser.get(t).getAvg_dur_at_saturday() + ","
					+ arrUser.get(t).getAvg_dur_at_friday() + ","
					+ arrUser.get(t).getAvg_dur_at_workday() + ","

					+ arrUser.get(t).getNum_call_at_sunday_prob() + ","
					+ arrUser.get(t).getNum_call_at_Satday_prob() + ","
					+ arrUser.get(t).getNum_call_at_Friday_prob() + ","
					+ arrUser.get(t).getNum_call_at_Workday_prob() + ","

					+ arrUser.get(t).getAvg_len_at_sunday() + ","
					+ arrUser.get(t).getAvg_len_at_saturday() + ","
					+ arrUser.get(t).getAvg_len_at_friday() + ","
					+ arrUser.get(t).getAvg_len_at_workday() + ","

					+ arrUser.get(t).getNum_sms_at_sunday_prob() + ","
					+ arrUser.get(t).getNum_sms_at_Satday_prob() + ","
					+ arrUser.get(t).getNum_sms_at_Friday_prob() + ","
					+ arrUser.get(t).getNum_sms_at_Workday_prob() + ","

					+ diffage + "," + facebookfriend + "," + hasanniversary
					+ "," + workphone + "," + homephone + "," + email + ","
					+ note + "," + im + "," + samegender + "," + frequentseen
					+ "," + label + "\n");
		}
		fw.close();
		// ~-------------------------------------------STATISTICS----------------------------------------
		File fileDir2 = new File("callogs_stats.csv");

		Writer out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(fileDir2), "UTF-8"));

		// FileWriter fw2 = new FileWriter(new File("callogs_stats.csv"));

		// fw2.write("\"\",\"Gia đình\",\"Công việc\",\"Người quen\",\"Bạn bè\",\"Khác\",\n");
		out.append("\"\",\"Gia_Dinh\",\"Cong_Viec\",\"Xa_hoi\",\"Ban_be\"\n");
		for (int i1 = 0; i1 < 24; i1++) {
			out.append(i1 + "," + arrStatistic.get(0).getCallHour()[i1] + ","
					+ arrStatistic.get(1).getCallHour()[i1] + ","
					+ arrStatistic.get(2).getCallHour()[i1] + ","
					+ arrStatistic.get(3).getCallHour()[i1] + "\n");
		}
		out.flush();
		out.close();

		// -----------------------------------\

		FileWriter fw3 = new FileWriter(new File("smslogs_stats.csv"));
		fw3.write("\"\",\"Gia_Dinh\",\"Cong_Viec\",\"Xa_hoi\",\"Ban_be\"\n");
		for (int i1 = 0; i1 < 24; i1++) {
			fw3.write(i1 + "," + arrStatistic.get(0).getSmsHour()[i1] + ","
					+ arrStatistic.get(1).getSmsHour()[i1] + ","
					+ arrStatistic.get(2).getSmsHour()[i1] + ","
					+ arrStatistic.get(3).getSmsHour()[i1] + "\n");
		}
		fw3.close();

		// -----------------------------------------------------------
		FileWriter fw4 = new FileWriter(new File("relationCompare.csv"));
		fw4.write("\"\",\"Gia_Dinh\",\"Cong_Viec\",\"Xa_hoi\",\"Ban_be\"\n");
		fw4.write("CALL" + "," + arrStatistic.get(0).getCallnum() + ","
				+ arrStatistic.get(1).getCallnum() + ","
				+ arrStatistic.get(2).getCallnum() + ","
				+ arrStatistic.get(3).getCallnum() + "\n");
		fw4.write("SMS" + "," + arrStatistic.get(0).getSmsnum() + ","
				+ arrStatistic.get(1).getSmsnum() + ","
				+ arrStatistic.get(2).getSmsnum() + ","
				+ arrStatistic.get(3).getSmsnum() + "\n");
		fw4.close();
		// ------------------------------------------------
		FileWriter fw5 = new FileWriter(new File("calllogsDay_stats.csv"));
		fw5.write("\"\",\"Gia_Dinh\",\"Cong_Viec\",\"Xa_hoi\",\"Ban_be\"\n");
		for (int i1 = 0; i1 < 24; i1++) {
			fw5.write(i1 + "," + arrStatistic.get(0).getSmsHour()[i1] + ","
					+ arrStatistic.get(1).getSmsHour()[i1] + ","
					+ arrStatistic.get(2).getSmsHour()[i1] + ","
					+ arrStatistic.get(3).getSmsHour()[i1] + "\n");
		}
		fw5.close();
		// --------------------------------------------------------
		FileWriter fw6 = new FileWriter(new File("calllogs_statsf.csv"));
		fw6.write("\"ID\",\"DateTime\",\"Duration\",\"Call Type\",\"Label\"\n");
		for (int i1 = 0; i1 < arrUser.size(); i1++) {
			for (int j1 = 0; j1 < arrUser.get(i1).getArrCallLogs().size(); j1++) {
				fw6.write(arrUser.get(i1).getImei()
						+ "-"
						+ arrUser.get(i1).getId()
						+ ","
						+ arrUser.get(i1).getArrCallLogs().get(j1).getDate()
								.toString()
						+ " "
						+ arrUser.get(i1).getArrCallLogs().get(j1).getTime()
								.toString()
						+ ","
						+ arrUser.get(i1).getArrCallLogs().get(j1)
								.getDuration()
						+ ","
						+ arrUser.get(i1).getArrCallLogs().get(j1)
								.getCalltype() + ","
						+ arrUser.get(i1).getRelationshipLabelProcessed()
						+ "\n");
			}
		}
		fw6.close();
		// --------------------------------------------------------
		FileWriter fw7 = new FileWriter(new File("smsogs_statsf.csv"));
		fw7.write("\"ID\",\"DateTime\",\"Lengh\",\"Sms Type\",\"Label\"\n");
		for (int i1 = 0; i1 < arrUser.size(); i1++) {
			for (int j1 = 0; j1 < arrUser.get(i1).getArrSmsLogs().size(); j1++) {
				fw7.write(arrUser.get(i1).getImei()
						+ "-"
						+ arrUser.get(i1).getId()
						+ ","
						+ arrUser.get(i1).getArrSmsLogs().get(j1).getDate()
								.toString()
						+ " "
						+ arrUser.get(i1).getArrSmsLogs().get(j1).getTime()
								.toString() + ","
						+ arrUser.get(i1).getArrSmsLogs().get(j1).getLength()
						+ ","
						+ arrUser.get(i1).getArrSmsLogs().get(j1).getSmstype()
						+ "," + arrUser.get(i1).getRelationshipLabelProcessed()
						+ "\n");
			}
		}
		fw7.close();
		// ~CALL/SMS RANK
		// report-----------------------------------------------
		FileWriter fw8 = new FileWriter(new File("Call_SMS_Rank_Report.csv"));
		fw8.write("\"Call_Rank\",\"SMS_Rank\",\"Label\"\n");
		for (int i1 = 0; i1 < arrUser.size(); i1++) {
			fw8.write("Rank " + arrUser.get(i1).getCallrank() + "," + "Rank "
					+ arrUser.get(i1).getSmsrank() + ","
					+ arrUser.get(i1).getRelationshipLabelProcessed() + "\n");

		}
		fw8.close();
		// RelationCount-------------------------------------------------------------
		FileWriter fw9 = new FileWriter(new File("RelationStatistic.csv"));
		fw9.write("\"Label\"\n");
		for (int i1 = 0; i1 < arrUserStats.size(); i1++) {
			fw9.write(arrUserStats.get(i1).relationshipLabelProcessing2()
					+ "\n");

		}
		fw9.close();

	}

	/**
	 * Kiem tra thoi gian (6 thang)
	 * 
	 * @param aDate
	 * @return
	 */
	public boolean checkTime(Date aDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -6);
		return aDate.compareTo(calendar.getTime()) < 0;
	}

	public boolean checkUsefulParticipant(Participant part) {
		Date oldestCall, oldestSms;
		for (int r = 0; r < part.getArraypUser().size(); r++) {
			part.getArraypUser().get(r).sortAscendingCallByDate();

		}
		return false;
	}

	public void CSVWriter() throws Exception {
		CSVSaver csvs = new CSVSaver();
		String[] options = { "-F" };
		csvs.setOptions(options);

	}
}
